home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_02 / 1102086a < prev    next >
Text File  |  1992-12-12  |  11KB  |  316 lines

  1.        /***********************************************
  2.        *
  3.        *   file d:\cips\mainseg.c
  4.        *
  5.        *   Functions: This file contains
  6.        *      main
  7.        *
  8.        *   Purpose:
  9.        *      This file contains the main calling
  10.        *      routine in an edge detection program.
  11.        *
  12.        *   External Calls:
  13.        *      gin.c - get_image_name
  14.        *      numcvrt.c - get_integer
  15.        *                  int_convert
  16.        *      tiff.c - read_tiff_header
  17.        *                enhance_edges
  18.        *      hist.c - zero_histogram
  19.        *               smooth_histogram
  20.        *               show_histogram
  21.        *               calculate_histogram
  22.        *      segment.c - threshold_image_array
  23.        *                  grow
  24.        *                  find_peaks
  25.        *                  peaks_high_low
  26.        *                  valley_high_low
  27.        *                  threshold_and_find_means
  28.        *
  29.        *   Modifications:
  30.        *      27 September 1992 - created
  31.        *
  32.        *************************************************/
  33.  
  34. #include "cips.h"
  35.  
  36. short the_image[ROWS][COLS];
  37. short out_image[ROWS][COLS];
  38. unsigned long histogram[GRAY_LEVELS+1];
  39.  
  40. main(argc, argv)
  41.    int argc;
  42.    char *argv[];
  43. {
  44.    char   name[80], name2[80], response[80];
  45.    int    count, i, ie, il, j, k, le, length, ll,
  46.           peak1, peak2, size,
  47.           t, type, v, width;
  48.    short  background, hi, low, object, value;
  49.    struct tiff_header_struct image_header;
  50.  
  51.    _setvideomode(_TEXTC80); /* MSC 6.0 statements */
  52.    _setbkcolor(1);
  53.    _settextcolor(7);
  54.    _clearscreen(_GCLEARSCREEN);
  55.  
  56.    if(argc < 7){
  57.     printf("\n\nmainseg in-file out-file hi low value operation");
  58.     printf("\n\t\toperation = threshold grow peaks valleys adaptive");
  59.     printf("\n");
  60.     exit(0);
  61.    }
  62.  
  63.    strcpy(name, argv[1]);
  64.    strcpy(name2, argv[2]);
  65.    hi    = atoi(argv[3]);
  66.    low   = atoi(argv[4]);
  67.    value = atoi(argv[5]);
  68.  
  69.    il = 1;
  70.    ie = 1;
  71.    ll = ROWS+1;
  72.    le = COLS+1;
  73.  
  74.    read_tiff_header(name, &image_header);
  75.  
  76.    length = (90 + image_header.image_length)/ROWS;
  77.    width  = (90 +image_header.image_width)/COLS;
  78.    count  = 1;
  79.    printf("\nlength=%d  width=%d", length, width);
  80.  
  81.    if(does_not_exist(name2)){
  82.       read_tiff_header(name, &image_header);
  83.       round_off_image_size(&image_header,
  84.                            &length, &width);
  85.       image_header.image_length = length*ROWS;
  86.       image_header.image_width  = width*COLS;
  87.       create_allocate_tiff_file(name2, &image_header,
  88.                                 out_image);
  89.    }  /* ends if does_not_exist */
  90.    zero_histogram(histogram);
  91.  
  92.  
  93.       /*********************************
  94.       *
  95.       *   Manual Threshold operation
  96.       *
  97.       *********************************/
  98.  
  99.   if(argv[6][0] == 't'){
  100.      for(i=0; i<length; i++){
  101.         for(j=0; j<width; j++){
  102.           printf("\nrunning %d of %d", count, length*width);
  103.           count++;
  104.              read_tiff_image(name, the_image,
  105.                   il+i*ROWS, ie+j*COLS,
  106.                   ll+i*ROWS, le+j*COLS);
  107.              printf("\nMS> Calling threshold");
  108.              threshold_image_array(the_image, out_image,
  109.                         hi, low, value);
  110.              write_array_into_tiff_image(name2, out_image,
  111.                               il+i*ROWS,
  112.                               ie+j*COLS,
  113.                               ll+i*ROWS,
  114.                               le+j*COLS);
  115.         }  /* ends loop over i */
  116.      }  /* ends loop over j */
  117.   }  /* ends if t */
  118.  
  119.       /*********************************
  120.       *
  121.       *   Grow region operation
  122.       *
  123.       *********************************/
  124.  
  125.   if(argv[6][0] == 'g'){
  126.      for(i=0; i<length; i++){
  127.         for(j=0; j<width; j++){
  128.           printf("\nrunning %d of %d", count, length*width);
  129.           count++;
  130.              read_tiff_image(name, the_image,
  131.                   il+i*ROWS, ie+j*COLS,
  132.                   ll+i*ROWS, le+j*COLS);
  133.              printf("\nMS> Calling grow");
  134.              grow(the_image, value);
  135.              write_array_into_tiff_image(name2, the_image,
  136.                                     il+i*ROWS,
  137.                                     ie+j*COLS,
  138.                                     ll+i*ROWS,
  139.                                     le+j*COLS);
  140.         }  /* ends loop over i */
  141.      }  /* ends loop over j */
  142.     }  /* ends if g */
  143.  
  144.       /*********************************
  145.       *
  146.       *   Peak threshold operation
  147.       *
  148.       *********************************/
  149.  
  150.    if(argv[6][0] == 'p'){
  151.  
  152.        /* calculate histogram for the
  153.           entire image file */
  154.  
  155.       zero_histogram(histogram);
  156.       for(i=0; i<length; i++){
  157.          for(j=0; j<width; j++){
  158.             printf("\nrunning %d of %d", count, length*width);
  159.             count++;
  160.             read_tiff_image(name, the_image,
  161.                     il+i*ROWS, ie+j*COLS,
  162.                     ll+i*ROWS, le+j*COLS);
  163.             printf("\nMS> Calling hist functions");
  164.             calculate_histogram(the_image, histogram);
  165.          }  /* ends loop over i */
  166.       }  /* ends loop over j */
  167.  
  168.       smooth_histogram(histogram);
  169.       show_histogram(histogram);
  170.       find_peaks(histogram, &peak1, &peak2);
  171.       printf("\npeak1=%d  peak2=%d", peak1, peak2);
  172.       peaks_high_low(histogram, peak1, peak2,
  173.                      &hi, &low);
  174.       printf("\nhi=%d low=%d", hi, low);
  175.  
  176.               /* now read the image file again
  177.                  and threshold and grow objects. */
  178.       count = 1;
  179.       for(i=0; i<length; i++){
  180.          for(j=0; j<width; j++){
  181.             printf("\nrunning %d of %d", count, length*width);
  182.             count++;
  183.             read_tiff_image(name, the_image,
  184.                    il+i*ROWS, ie+j*COLS,
  185.                    ll+i*ROWS, le+j*COLS);
  186.             threshold_image_array(the_image, out_image,
  187.                                   hi, low, value);
  188.             write_array_into_tiff_image(name2, out_image,
  189.                                il+i*ROWS,
  190.                                ie+j*COLS,
  191.                                ll+i*ROWS,
  192.                                le+j*COLS);
  193.          }  /* ends loop over i */
  194.       }  /* ends loop over j */
  195.    }  /* ends if p */
  196.  
  197.       /*********************************
  198.       *
  199.       *   Valley threshold operation
  200.       *
  201.       *********************************/
  202.  
  203.    if(argv[6][0] == 'v'){
  204.  
  205.        /* calculate histogram for the
  206.           entire image file */
  207.  
  208.       zero_histogram(histogram);
  209.       for(i=0; i<length; i++){
  210.          for(j=0; j<width; j++){
  211.             printf("\nrunning %d of %d", count, length*width);
  212.             count++;
  213.             read_tiff_image(name, the_image,
  214.                     il+i*ROWS, ie+j*COLS,
  215.                     ll+i*ROWS, le+j*COLS);
  216.             printf("\nMS> Calling hist functions");
  217.             calculate_histogram(the_image, histogram);
  218.          }  /* ends loop over i */
  219.       }  /* ends loop over j */
  220.  
  221.  
  222.       smooth_histogram(histogram);
  223.       show_histogram(histogram);
  224.       find_peaks(histogram, &peak1, &peak2);
  225.       printf("\npeak1=%d  peak2=%d", peak1, peak2);
  226.       valley_high_low(histogram, peak1, peak2,
  227.                       &hi, &low);
  228.       printf("\nhi=%d low=%d", hi, low);
  229.  
  230.               /* now read the image file again
  231.                  and threshold and grow objects. */
  232.       count = 1;
  233.       for(i=0; i<length; i++){
  234.          for(j=0; j<width; j++){
  235.             printf("\nrunning %d of %d", count, length*width);
  236.             count++;
  237.             read_tiff_image(name, the_image,
  238.                    il+i*ROWS, ie+j*COLS,
  239.                    ll+i*ROWS, le+j*COLS);
  240.             threshold_image_array(the_image, out_image,
  241.                                   hi, low, value);
  242.             write_array_into_tiff_image(name2, out_image,
  243.                                il+i*ROWS,
  244.                                ie+j*COLS,
  245.                                ll+i*ROWS,
  246.                                le+j*COLS);
  247.          }  /* ends loop over i */
  248.